package net.pierrerenaudin.blobbyvolley.client;

public class Vector {

	public double x;
	public double y;


	public Vector(double d, double e) {
		this.x = d;
		this.y = e;
	}


	public void vectorByDots(Vector dot1, Vector dot2)
	{
		this.x = dot2.x - dot1.x;
		this.y = dot2.y - dot1.y;
	}

	public Vector halfVector(Vector vec) {
		return new Vector(
				this.getX() + (vec.getX() - this.getX()) / 2,
				this.getY() + (vec.getY() - this.getY()) / 2
		);
	}

	public boolean equals(Vector vec) {
		return vec.getX() == this.getX() && vec.getY() == this.getY();
	};

	public Vector addVector(Vector vec) {
		return new Vector(this.getX() + vec.getX(), this.getY() + vec.getY());
	};

	public Vector subtractVector(Vector vec) {
		return new Vector(this.getX() - vec.getX(), this.getY() - vec.getY());
	};

	public Vector multiply(double dotProduct) {
		return new Vector(
				this.getX() * dotProduct,
				this.getY() * dotProduct
		);
	}

	public Vector multiplyVector(Vector vec) {
		return new Vector(
				this.getX() * vec.getX(),
				this.getY() * vec.getY()
		);
	}

	public Vector divide(int scalar) {
		return new Vector(
				this.getX() / scalar,
				this.getY() / scalar
		);
	}

	public Vector invert() {
		return new Vector(
				-this.getX(),
				-this.getY()
		);
	}

	public double dotProduct(Vector vec) {
		return this.getX() * vec.getX() + this.getY() * vec.getY();
	}

	public double crossProduct(Vector vec) {
		return this.getX() * vec.getY() - this.getY() * vec.getX();
	}

	public Vector reflect(Vector vec) {
		Vector newVec     = vec.multiply(2);
		double dotProduct = this.dotProduct(vec);
		newVec = newVec.multiply(dotProduct);
		return this.subtractVector(newVec);
	}

	public Vector reflectX(){
		return new Vector(-this.getX(), this.getY());
	}

	public Vector reflectY() {
		return new Vector(this.getX(), -this.getY());
	}

	public Vector scale(double factor) {
		return new Vector(
				this.getX() * factor,
				this.getY() * factor
		);
	}

	public Vector scaleX(double factor) {
		return new Vector(
				this.getX() * factor,
				this.getY()
		);
	}

	public Vector scaleY(double factor) {
		return new Vector(
				this.getX(),
				this.getY() * factor
		);
	}

	public double getLength() {
		return Math.sqrt(Math.pow(this.getX(), 2.0) + Math.pow(this.getY(), 2.0));
	}

	public Vector normalise() {
		double fLength = this.getLength();
		if (fLength > 1e-08)
			return new Vector(this.getX() / fLength, this.getY() / fLength);	
		return (Vector) this.clone(); // Might cause issues in IE.
	}

	public Vector clone() {
		return new Vector(this.getX(), this.getY());
	}

	public Vector contraVector() {
		return this.invert();
	}

	public boolean lessThan(Vector vector) {
		return (this.getX() > vector.getX() && this.getY() > vector.getY());
	};

	public void clear() {
		this.x = 0.0;
		this.y = 0.0;
	}


	public double getX() {
		return x;
	}


	public void setX(int x) {
		this.x = x;
	}


	public double getY() {
		return y;
	}


	public void setY(int y) {
		this.y = y;
	}


}
